﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace System.Web.Mvc.Ajax
{
    /// <summary>
    /// 为AjaxHelper添加Pagger扩展方法
    /// </summary>
    public static class AjaxHelperExtensions
    {
        /// <summary>
        /// Ajax分页扩展方法
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="ajaxPaggerFormat">生成Ajax分页对象的对象</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, IPaggerFormat ajaxPaggerFormat)
        {
            if (ajaxPaggerFormat == null)
            {
                throw new ArgumentNullException();
            }

            (ajaxPaggerFormat as AjaxPaggerFormat).Helper = helper;

            string s = ajaxPaggerFormat.GetFormatString(actionName, controllerName, routeValues, rowCount, pageIndex, pageSize);
            s = String.Format("<div class=\"pagger-container\">{0}</div>", s);
            return new MvcHtmlString(s);
        }

        /// <summary>
        /// Ajax分页扩展方法
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            PageButtonLabel bl = buttonLabel == null ? PageButtonLabel.Default : buttonLabel;
            IPaggerFormat paggerFormat = null;
            switch (paggerType)
            {
                case AjaxPaggerType.Numeric:
                    paggerFormat = new AjaxNumericPaggerFormat(options)
                    {
                        PreviousLabel = bl.Previous,
                        NextLabel = bl.Next
                    };
                    break;
                case AjaxPaggerType.More:
                    paggerFormat = new AjaxMorePaggerFormat(options)
                    {
                        MoreLabel = bl.More
                    };
                    break;
                default:
                    paggerFormat = new AjaxPrevNextPaggerFormat(options)
                    {
                        FirstLabel = bl.First,
                        PreviousLabel = bl.Previous,
                        NextLabel = bl.Next,
                        LastLabel = bl.Last
                    };
                    break;
            }
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, paggerFormat);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, paggerType, options, buttonLabel);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller并且不带路由参数
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            return Pagger(helper, actionName, null, rowCount, pageIndex, pageSize, paggerType, options, buttonLabel);
        }

        /// <summary>
        /// Ajax分页扩展方法
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options)
        {
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, paggerType, options, PageButtonLabel.Default);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options)
        {
            string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, paggerType, options);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller并且不带路由参数
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>
        /// <param name="paggerType">分页方式</param>
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, int rowCount, int pageIndex, int pageSize, AjaxPaggerType paggerType, AjaxOptions options)
        {
            return Pagger(helper, actionName, null, rowCount, pageIndex, pageSize, paggerType, options);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用默认的PrevNext分页方式
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, AjaxPaggerType.PrevNext, options, buttonLabel);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller和PrevNext分页方式
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, options, buttonLabel);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用PrevNext分页方式和当前的Controller并且不带路由参数
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>
        /// <param name="buttonLabel">分页按钮的Label</param>
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, int rowCount, int pageIndex, int pageSize, AjaxOptions options, PageButtonLabel buttonLabel)
        {
            return Pagger(helper, actionName, null, rowCount, pageIndex, pageSize, options, buttonLabel);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用PrevNext分页方式和默认的分页按钮Label
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, string controllerName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxOptions options)
        {
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, options, PageButtonLabel.Default);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller和PrevNext分页方式和默认的分页按钮Label
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>        
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, RouteValueDictionary routeValues, int rowCount, int pageIndex, int pageSize, AjaxOptions options)
        {
            string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
            return Pagger(helper, actionName, controllerName, routeValues, rowCount, pageIndex, pageSize, options);
        }

        /// <summary>
        /// Ajax分页扩展方法，使用当前的Controller并且不带路由参数，使用PrevNext分页方式和默认的分页按钮Label
        /// </summary>
        /// <param name="helper">AjaxHelper对象</param>
        /// <param name="actionName">获取分页数据的Action Name</param>
        /// <param name="controllerName">获取分页数据的Controller Name</param>
        /// <param name="routeValues">获取分页数据的路由参数</param>
        /// <param name="rowCount">分页数据的总行数</param>
        /// <param name="pageIndex">当前分页索引，从0开始</param>
        /// <param name="pageSize">每页的显示个数</param>        
        /// <param name="options">AjaxHelper.AjaxOptions对象</param>        
        /// <returns></returns>
        public static MvcHtmlString Pagger(this AjaxHelper helper, string actionName, int rowCount, int pageIndex, int pageSize, AjaxOptions options)
        {
            return Pagger(helper, actionName, null, rowCount, pageIndex, pageSize, options);
        }
    }
}
